<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace api\controllers;

use Yii;

/**
 * Description of RegisterController
 *
 * @author Administrator
 */
class ForgotController extends \api\components\Controller {

    public function behaviors() {
        return [
            'verbs' => [
                'class'   => \yii\filters\VerbFilter::className(),
                'actions' => [
                    'index' => ['post'],
                ],
            ],
        ];
    }

    public function actionIndex() {

        $mobile    = Yii::$app->request->post('mobile');
        $password  = Yii::$app->request->post('password');
        $smscode   = Yii::$app->request->post('smscode');
        
        /*
         * 检查手机验证码
         */
        $validator = new \common\models\SmsValidator();
        list($ret, $msg) = $validator->validateCode($mobile, 'forgot', $smscode);
        if ($ret !== TRUE) {
            return ['code' => $ret, 'message' => $msg];
        }
        
        $data = array();
        $user = \yiike\user\models\User::findOne(['username' => $mobile]);
        if ($user) {
            if ($user->resetPassword($password)) {
                $data = ['code' => 0];
            }
            else {
                $data = ['code' => 100032]; 
            }
        }
        else {
            $data = ['code' => 100031];
        }
        return $data;
    }


}
